const Service = require('egg').Service;
const md5 = require('utility').md5
const jwt = require('jsonwebtoken')
class HomeService extends Service {
  async getList() {
    const {ctx,app} = this;
    const {pageIndex,pageSize,keyword} =ctx.query;
    // 分页 模糊查询
    let sql = `select * from shops where 1=1 `
    let sqlCount = `select count(id) as total from shops where 1=1 `
    if(keyword){
      sql+= `and title like "%${keyword}%" `
      sqlCount += `and title like "%${keyword}%" `
    }
    if(pageIndex&&pageSize){
      sql+=`limit ${(pageIndex-1)*pageSize},${pageSize}`
    }
    const data = await app.mysql.query(sql);
    const [{total}] = await app.mysql.query(sqlCount);
    return {
      data,
      total
    }
  }
  async getDetail() {
    const {ctx,app} = this;
    const data = await app.mysql.get('shops', {id:ctx.params.id})
    return data;
  }
}

module.exports = HomeService;